package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.saas.domain.dao.LikeDao;
import com.xbongbong.saas.domain.entity.LikeEntity;
import com.xbongbong.saas.model.LikeModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/27 19:55
 * @since v1.0
 */
@Service
public class LikeModelImpl implements LikeModel {
    @Resource
    private LikeDao likeDao;
    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return likeDao.getEntitysCount(map);
    }

    @Override
    public void save(LikeEntity likeEntity) {
        if (likeEntity.getId() == null){
            likeDao.insert(likeEntity);
        }else {
//            update(likeEntity);
        }
    }

    @Override
    public List<LikeEntity> findEntitys(Map<String, Object> map) {
        return likeDao.findEntitys(map);
    }

    @Override
    public Map<Long, Integer> getLikeCountMap(Collection<Long> dataIdIn, String corpid, Integer code) {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del", 0);
        map.put("type", code);
        map.put("refIdIn", dataIdIn);
        List<LikeEntity> likeEntityList = findEntitys(map);
        Map<Long, Integer> likeCountMap = new HashMap<>(likeEntityList.size());
        for (LikeEntity entityExt : likeEntityList) {
            likeCountMap.put(entityExt.getRefId(), likeCountMap.getOrDefault(entityExt.getRefId(), 0) + 1);
        }
        return likeCountMap;
    }

    @Override
    public List<Long> hasMyLike(Collection<Long> dataIdIn, String corpid, Integer type, String userId) {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del", 0);
        map.put("refIdIn", dataIdIn);
        map.put("type", type);
        map.put("userId", userId);
        map.put("groupByStr", "ref_id");
        map.put("columns", "ref_id");
        List<LikeEntity> likeEntityList = findEntitys(map);
        List<Long> refIdList = new ArrayList<>();
        for (LikeEntity entityExt : likeEntityList) {
            refIdList.add(entityExt.getRefId());
        }
        return refIdList;
    }

    @Override
    public Integer dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) {
        return likeDao.dataConsistencyUpdateUser(corpid, userId, userName, userAvatar);
    }

    @Override
    public void insertBatch(List<LikeEntity> list) {
        likeDao.insertBatch(list);
    }
}
